<template>
  <div class="dashboard-container">
    <common-view
      :table-prop="tableProp"
      :form-rules="formRules"
      module-name="academyConfig"
      :search="getAcademyList"
      :add="addAcademy"
      :delete="deleteAcademy"
      :update="updateAcademy"
      :get-form-data="getFormData"
      :get-search-data="getSearchData"
    />

  </div>
</template>

<script>

import CommonView from '@/components/commonView/index'
import { getAcademyList, addAcademy, deleteAcademy, updateAcademy } from '@/api/academy'
import { getUserList } from '@/api/user'

export default {
  name: 'AcademyConfig',
  components: { 'common-view': CommonView },
  data() {
    return {
      getAcademyList,
      addAcademy,
      deleteAcademy,
      updateAcademy,
      userList: [],
      tableProp: [
        {
          prop: 'academyName',
          label: '学院名称'
        },
        {
          prop: 'counselorId',
          label: '辅导员'
          // formatter: value => {
          //   const findUser = this.userList.find((user) => user.id === value.counselorId)
          //   return findUser ? findUser.nickName : '-'
          // }
        }, {
          prop: 'deanId',
          label: '院长'
          // formatter: value => {
          //   const findUser = this.userList.find((user) => user.id === value.deanId)
          //   return findUser ? findUser.nickName : '-'
          // }
        },
        {
          prop: 'associateDeanId',
          label: '副院长'
          // formatter: value => {
          //   const findUser = this.userList.find((user) => user.id === value.associateDeanId)
          //   return findUser ? findUser.nickName : '-'
          // }
        }, {
          prop: 'createTime',
          label: '创建时间'
        }, {
          prop: 'updateTime',
          label: '修改时间'
        }
      ],
      formRules: {
        academyName: [
          { required: true, message: '请输入学院名称', trigger: 'change' }
        ],
        counselorId: [
          { required: true, message: '请选择辅导员', trigger: 'change' }
        ],
        deanId: [
          { required: true, message: '请选择院长', trigger: 'change' }
        ],
        associateDeanId: [
          { required: true, message: '请选择副院长', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    // 数据初始化
    this.init()
  },
  methods: {
    async init() {
      const [userList] = await Promise.all([getUserList({})])
      this.userList = userList.list
    },
    // 新增、编辑表单转换
    getFormData(formData, handle, id) {
      console.log('转换前', formData)
      const { academyName, counselorId, deanId, associateDeanId } = formData
      const newFormData = { academyName, counselorId, deanId, associateDeanId }
      if (handle === 'edit') {
        newFormData['id'] = id
      }
      return newFormData
    },
    // 查询条件转换
    getSearchData(formData) {
      const { academyName } = formData
      return {
        academyName
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
